// pages/newsDetail/newsDetail.js
var app = getApp();
var fake_comments = require("../../data/data_comments.js");
var url = " https://www.hkzhaoscut.top/"
// 这是数据库通信提交新的评论信息
var submitComment = function(comment) {
  wx.request({
    url: url,
    method: "post",
    data: {

    }
  })
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: 0,
    scrollTop: 0,
    comment: "",
    commentList: [],
    // 用于展开评论的变量
    hideComment:[],
    // 用于判断当前用户是否对某一条评论点赞的数组
    thumbUp:[]
  },
  // 对于回复他人的评论，采取更新的方法，更新该数组下的replyComment属性
  // 提交评论
  submit(e) {
    // 先检查登陆状态
    if (app.globalData.userType != '') {
      var that = this
      var comment = that.data.comment
      if (comment == "") {
        wx.showToast({
          title: '评论不能为空！',
          icon: 'none',
        })
      } else {
        // 提交评论
        console.log("评论内容为:" + comment)
        submitComment(e)
        // 本地也需更新评论信息，需要登录后的用户id认证、名字和头像
      }
    } else {
      // 跳转到登录页面
    }

  },
  // 监听输入框
  inputComment(e) {
    this.setData({
      comment: e.detail.value
    })
  },
  // 监听页面滚动
  onPageScroll(e) {
    var that = this
    that.setData({
      scrollTop: e.scrollTop
    })
  },

  //  点赞
  thumpUpComment(e) {
    var that = this
    var index = e.currentTarget.id
    var thumbUp = !that.data.thumbUp[index]
    if (thumbUp == false) {
      var thumbUpCount = that.data.commentList[index].thumbUpCount - 1
    } else {
      var thumbUpCount = that.data.commentList[index].thumbUpCount + 1
    }
    var setThumbUp = 'thumbUp[' + index + ']'
    var setThumpUpCount = 'commentList[' + index + '].thumbUpCount'
    that.setData({
      [setThumpUpCount]: thumbUpCount,
      [setThumbUp]:thumbUp
    })
  },
  // 展开该评论所有的回复
  commentDetail(e) {
    var index=e.currentTarget.dataset.idx
    var that=this
    // console.log(index)
    var hideComment = !that.data.hideComment[index]
    var setHideComment = 'hideComment[' + index + ']'
    that.setData({
      [setHideComment]: hideComment
    })
   
  },
  // 举报
  showJubao() {
    wx.showModal({
      title: '举报',
      content: '确定举报该评论吗？',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '感谢你的反馈！',
            icon: 'none',
          })
        } 
      }
    })
  },
  onReady(e) {
    var that = this
    wx.createSelectorQuery().select("#news-detail").boundingClientRect(function(e) {
      that.setData({
        scrollHeight: e.height
      })
    }).exec()
  },
  onLoad(e) {
    var that = this
    var thumbUp=new Array(1000)
    var hideComment= new Array(1000)
    for(var index=0;index<1000;index++){
      thumbUp[index]=false
      hideComment[index]=true
    }
    that.setData({
      commentList: fake_comments.data.commentList,
      hideComment: hideComment,
      thumbUp: thumbUp
    })
  }
})